package com.example.practice;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.practice.view.CourseView;
import com.example.practice.view.ExerciseViewYL;
import com.example.practice.view.MyInfoViewYl;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

//    中间内容栏
    private FrameLayout bodyLayout;

//    底部按钮栏
    public LinearLayout bottomLayout;

//    底部按钮
    private View courseBtn;
    private View exercisesBtn;
    private View myInfoBtn;
    private TextView tv_course;
    private TextView tv_exercises;
    private TextView tv_myInfo;
    private ImageView iv_course;
    private ImageView iv_exercises;
    private ImageView iv_myInfo;
    private TextView tv_back, tv_title;
    private RelativeLayout rl_title_bar;
    private MyInfoViewYl myInfoViewYl;
    private ExerciseViewYL exerciseViewYL;
    private CourseView courseView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        initTitleBar();
        initBodyLayout();
        initBottomBar();
        setInitState();
        setListener();
    }

    private void setListener() {
        for (int i=0; i<bottomLayout.getChildCount(); i++){
            bottomLayout.getChildAt(i).setOnClickListener(this);
        }
    }

    private void setInitState() {
        clearBottomSelectState();
        setSelectStatus(0);
        createView(0);
    }

    /**
     * 显示对应的页面
     */
    private void selectDisplayView(int i){
        removeAllView();
        createView(i);
        setSelectStatus(i);
    }

    private void initBottomBar() {
        bottomLayout=findViewById(R.id.bottom_bar);
        courseBtn=findViewById(R.id.bottom_course_btn_yl);
        exercisesBtn=findViewById(R.id.bottom_exercises_btn_yl);
        myInfoBtn=findViewById(R.id.bottom_my_btn_yl);
        tv_course=findViewById(R.id.bottom_bar_course_text);
        tv_exercises=findViewById(R.id.bottom_bar_exercises_text);
        tv_myInfo=findViewById(R.id.bottom_bar_my_text);
        iv_course=findViewById(R.id.bottom_bar_course_image);
        iv_exercises=findViewById(R.id.bottom_bar_exercises_image);
        iv_myInfo=findViewById(R.id.bottom_bar_my_image);
    }

    private void initBodyLayout() {
        bodyLayout=findViewById(R.id.main_body_yl);

    }

    private void initTitleBar() {
        tv_back=findViewById(R.id.tv_back);
        tv_title=findViewById(R.id.tv_title);
        tv_title.setText("博学谷课程");
        rl_title_bar=findViewById(R.id.rl_bar);
        rl_title_bar.setBackgroundColor(Color.parseColor("#60b4f9"));
        tv_back.setVisibility(View.GONE);
    }

    /**
     * 设置tabbar选中状态
     * @param index
     */
    public void setSelectStatus(int index){
        switch (index){
            case 0:
                courseBtn.setSelected(true);
                iv_course.setImageResource(R.drawable.main_course_icon_selected);
                tv_course.setTextColor(Color.parseColor("#60b4f9"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_title.setText("博学谷课程");
                break;
            case 1:
                exercisesBtn.setSelected(true);
                iv_exercises.setImageResource(R.drawable.main_exercises_icon_selected);
                tv_exercises.setTextColor(Color.parseColor("#60b4f9"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_title.setText("博学谷习题");
                break;
            case 2:
                myInfoBtn.setSelected(true);
                iv_myInfo.setImageResource(R.drawable.main_my_icon_selected);
                tv_myInfo.setTextColor(Color.parseColor("#60b4f9"));
                rl_title_bar.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 清楚底部选择状态
     */
    private void clearBottomSelectState() {
        tv_course.setTextColor(Color.parseColor("#666666"));
        tv_exercises.setTextColor(Color.parseColor("#666666"));
        tv_myInfo.setTextColor(Color.parseColor("#666666"));
        iv_course.setImageResource(R.drawable.main_course_icon);
        iv_exercises.setImageResource(R.drawable.main_exercises_icon);
        iv_myInfo.setImageResource(R.drawable.main_my_icon);
        for (int i=0;i<bottomLayout.getChildCount();i++){
            bottomLayout.getChildAt(i).setSelected(false);
        }
    }

    /**
     * 创建显示视图
     * @param viewIndex
     */
    private void createView(int viewIndex){
        switch (viewIndex){
            case 0:
                //课程界面
                if (courseView==null)//判断课程列表页面是否存在，不存在创建，存在获取
                {
                    courseView=new CourseView(this);//创建
                    bodyLayout.addView(courseView.getView());//将课程页面添加到mBodyLayout布局中
                }
                else
                    courseView.getView();//获取课程列表页面
                    courseView.showView();//显示课程列表页面
                break;
            case 1:
                if (exerciseViewYL==null){
                    exerciseViewYL=new ExerciseViewYL(this);
                    bodyLayout.addView(exerciseViewYL.getView());
                }
                else {
                    exerciseViewYL.getView();
                    exerciseViewYL.showView();
                }
                break;
            case 2:
                //我的界面
                if(myInfoViewYl==null){
                    myInfoViewYl=new MyInfoViewYl(this);
                    bodyLayout.addView(myInfoViewYl.getView());
                }
                else {
                    myInfoViewYl.getView();
                    myInfoViewYl.showView();
                }
                break;
        }
    }

    /**
     * 移除所有视图
     */

    private void removeAllView(){
        for (int i=0;i<bodyLayout.getChildCount();i++){
            bodyLayout.getChildAt(i).setVisibility(View.GONE);
        }
    }

    /**
     * 设置底部三个按钮点击事件
     */

    public void onClick(View v){
        switch (v.getId()){
            //课程点击事件
            case R.id.bottom_course_btn_yl:
                clearBottomSelectState();
                selectDisplayView(0);
                break;
            //习题点击事件
            case R.id.bottom_exercises_btn_yl:
                clearBottomSelectState();
                selectDisplayView(1);
                break;
            case R.id.bottom_my_btn_yl:
                clearBottomSelectState();
                selectDisplayView(2);
                break;
            default:
                break;
        }
    }

    /**
     * 清楚SP中的登录状态
     *
     */
    private void clearLoginStatus(){
        SharedPreferences sp = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isLogin", false);
        editor.putString("loginUserName","");
        editor.commit();
    }

    /**
     * 获取SP中的登陆状态
     */
    private boolean readLoginStatus(){
        SharedPreferences sp =getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        boolean isLogin=sp.getBoolean("isLogin",false);
        return isLogin;
    }

    protected long exitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime)>2000){
                Toast.makeText(MainActivity.this,"再按一次退出博学谷",Toast.LENGTH_SHORT).show();
                exitTime=System.currentTimeMillis();
            }
            else {
                MainActivity.this.finish();
                if(readLoginStatus()) {
                    clearLoginStatus();
                }
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    /**
     * 登录成功后回调方法
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //判断回传数据为空
        if(data!=null) {
            boolean isLogin=data.getBooleanExtra("isLogin",false);
            if (isLogin){
                clearBottomSelectState();
                selectDisplayView(0);
            }
            if (myInfoViewYl!=null) {
                myInfoViewYl.setLoginParams(isLogin);
            }
        }
    }
}